package pt.ipleiria.estg.es2.franciscofernandes.myinvitationonly;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import pt.ipleiria.estg.es2.franciscofernandes.myinvitationonly.modelo.Contacto;


public class MainActivity extends Activity {

    private boolean isChecked = false;
    private MenuItem item;
    private Contacto contacto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        obterContactoUtilizador();
    }

    private void obterContactoUtilizador() {

        contacto = new Contacto("Francisco", "2131089@my.ipleiria.pt");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_i_am_here) {
            invertCheckIm(item);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void invertCheckIm(final MenuItem item) {

        if (isChecked == false) {
            if (this.contacto != null && !contacto.getEmail().isEmpty() && !contacto.getName().isEmpty()) {

                AlertDialog.Builder constructor = new AlertDialog.Builder(this);

                constructor.setTitle(getString(R.string.confirmacao));
                constructor.setMessage(getString(R.string.tem_a_certeza) + getString(R.string.nome)
                        + contacto.getName() + getString(R.string.email) + contacto.getEmail()).setNegativeButton(getString(R.string.nao), null).setPositiveButton(getString(R.string.sim), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (temRede()) {
                            isChecked = true;
                            item.setIcon(R.drawable.ic_action_group);

                            Intent intent = new Intent(MainActivity.this, WhoIsHereActivity.class);
                            intent.putExtra(WhoIsHereActivity.IS_CHECKED_IN, isChecked);
                            startActivity(intent);
                        } else {


                        }

                    }
                });



            } else {
                Toast.makeText(this, getString(R.string.dados_incompletos), Toast.LENGTH_SHORT).show();
            }
        } else {
            isChecked = false;
            item.setIcon(R.drawable.ic_action_alone);
        }

        isChecked = !isChecked;
        item.setIcon(isChecked ? R.drawable.ic_action_group : R.drawable.ic_action_alone);

        if (isChecked) {
            Intent intent = new Intent(this, WhoIsHereActivity.class);
            intent.putExtra(WhoIsHereActivity.IS_CHECKED_IN, isChecked);
            startActivity(intent);
        }
    }

    private boolean temRede() {
        return true;
    }

}
